<?php 

class Report_model extends CI_Model{
	function __construct()
	{
	    parent::__construct();	    
	}
	
	//访问日志查询
	public function query_visits_report($querydata)
	{
		$sql="Select Date(dtDate) as visitDate,Count(distinct sIP) as VorIP,Count(iid) as PageView".
			" From his_operationlog ".
			" Where 1=1";
			
		if($querydata["BDate"]!="" )
		{
			$sql.=" And dtDate >= '".$querydata['BDate']."'";
		}
		else
		{
			$sql.=" And dtDate >= '".date("Y-m-d",strtotime("-7 day"))."'";
		}
		if($querydata["EDate"]!="")
		{
			$sql.=" And dtDate <= '".date("Y-m-d",strtotime($querydata['EDate']." +1 day"))."'";
		}
		$AgentCode=$this->session->userdata('uadmin_account');
		if($AgentCode!="")
		{
			$sql.=" And AgentCode = '".$AgentCode."'";
		}
		else
		{
			$sql.=" And AgentCode = '".$AgentCode."'";
		}
		
		$sql.=" group by visitDate";
		
		$query = $this->db->query($sql);
		
		$myreport = $query->result();
		
		return $myreport;
	}
	
	//访问日志查询明细
	public function query_visits_report_detail($querydata)
	{
		$AgenctCode;
		if($querydata["AgenctCode"]=="")
		{
			$AgenctCode=$this->session->userdata('uadmin_account');
		}
		$CurDate;
		if($querydata["CurDate"]!="" )
		{
			$CurDate=$querydata['CurDate'];
		}
		else
		{
			$CurDate=date("Y-m-d");
		}
		$sql="Select sIP,dtDate,sPage_name".
			" From his_operationlog ".
			" Where AgentCode='".$querydata["AgenctCode"]."'".
			" And dtDate >='".$CurDate."' and dtDate <= '".date("Y-m-d",strtotime($CurDate."+1 day"))."'".
			" order by dtdate desc";
		
		$query = $this->db->query($sql);
		
		$myreport = $query->result();
		
		return $myreport;
	}
	
	//统计订单
	public function query_orders_report($querydata)
	{
		$sql="SELECT BookDate,SalesName,PNR,departdate,ItineraryName,lPessengerName,TotalPrice FROM `map_order` WHERE Status='2' ";
			
		if($querydata["BDate"]!="" )
		{
			$sql.=" And BookDate >= '".$querydata['BDate']."'";
		}
		else
		{
			$sql.=" And BookDate >= '".date("Y-m-d",strtotime("-7 day"))."'";
		}
		if($querydata["EDate"]!="")
		{
			$sql.=" And BookDate <= '".date("Y-m-d",strtotime($querydata['EDate']."+1 day"))."'";
		}
		else
		{
			$sql.=" And BookDate <= '".date("Y-m-d",strtotime("+1 day"))."'";
		}
		
		$AgenctCode=$this->session->userdata('uadmin_account');
		
		if($AgenctCode!="")
		{
			$sql.=" And AgentCode = '".$AgenctCode."'";
		}
		else
		{
			$sql.=" And AgentCode = '".$AgenctCode."'";
		}
		
		$sql.=" order by BookDate ";
		
		$query = $this->db->query($sql);
		
		$myreport = $query->result();
		
		return $myreport;
	}
	
	//IBE统计报表
	public function querry_vIBE_used_report($querydata)
	{
		$BDate = $querydata["BDate"];
		if($BDate == "")
		{
			$BDate = date("Y-m-d",strtotime("-7 day"));
		}
		$EDate = $querydata["EDate"];
		if($EDate == "")
		{
			$EDate = date("Y-m-d");
		}
		$AgentCode = $querydata["AgentCode"];
		if($AgentCode =="")
		{
			$AgentCode=$this->db->query("select AuthorizedKey from sys_user where AgencyCode='".$this->session->userdata('uadmin_account')."'")->row()->AuthorizedKey;
		}
		else
		{
			$AgentCode=$this->db->query("select AuthorizedKey from sys_user where AgencyCode='".$AgentCode."'")->row()->AuthorizedKey;
		}
		
		$request='http://210.73.195.73:7655/hyservice.asmx/QueryAccountTime?';
		$request=$request.'sHashcode='.$AgentCode.
				'&StartDate='.$BDate.
				'&EndDate='.$EDate;
		$fdcontext=file_get_contents($request);	//读取远程数据
		//$fdcontext=file_get_contents(base_url().'js/testfd.xml');	//读取本地测试数据
		if(strpos($fdcontext,"无法识别的主机错误信息")>0)
		{
			exit("指定航段本日没有适用运价");
		}
		$fdcontext=str_ireplace("&lt;","<",$fdcontext);
		$fdcontext=str_ireplace("&gt;",">",$fdcontext);
		$fdcontext=str_ireplace("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<string xmlns=\"http://huitrip.com/\">","",$fdcontext);
		$fdcontext=str_ireplace("</string>","",$fdcontext);
			  	
		$fdcontext_dir = explode("|",$fdcontext);
		$tmplist=array();
		$result=array(count($fdcontext_dir)=>3);
		for($i=0;$i<count($fdcontext_dir);$i++)
		{
			$tmplist =  explode("/",$fdcontext_dir[$i]);
			$result[$i][0] = $tmplist[0];
			$result[$i][1] = $tmplist[1];
			$result[$i][2] = $tmplist[2];
		}
		
		return $result;
	}
	
	//获取客户下拉框列表内容
	public function get_Angcy_DropdownlistOption()
	{
		$sql="select AgencyCode,`Name` from sys_user where 1";
		$query = $this->db->query($sql);
		$AngcyListRs = $query->result();
		$AngcyData=array();
		foreach($AngcyListRs as $item)
		{
			$AngcyData[$item->AgencyCode] = $item->Name;
		}
		return $AngcyData;
	}
	
	public function get_Angcy_DropdownlistOption2()
	{
		$sql="select AuthorizedKey,`Name` from sys_user where 1";
		$query = $this->db->query($sql);
		$AngcyListRs = $query->result();
		$AngcyData=array();
		foreach($AngcyListRs as $item)
		{
			$AngcyData[$item->AuthorizedKey] = $item->Name;
		}
		return $AngcyData;
	}
}